package com.cskaoyan.service;

import com.cskaoyan.bean.User;
import com.cskaoyan.bean.user.Feedback;
import com.cskaoyan.mapper.FeedbackMapper;
import com.cskaoyan.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 姚旭佳
 */
@Service
public class FeedbackSubmitServiceImpl implements FeedbackSubmitService {
    @Autowired
    FeedbackMapper feedbackMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public void feedbackSubmit(Feedback feedback) {
        feedback.setUserId(getUserId());
        User user = userMapper.selectByPrimaryKey(getUserId());
        feedback.setUsername(user.getUsername());
        feedback.setAddTime(new Date());
        feedback.setUpdateTime(new Date());
        feedbackMapper.insertSelective(feedback);
    }

    private Integer getUserId(){
        Subject subject = SecurityUtils.getSubject();
        Object primaryPrincipal = subject.getPrincipals().getPrimaryPrincipal();
        return ((User) primaryPrincipal).getId();

    }
}
